<?php
/**
 * 轻舟CMS(QZLOG)
 *
 * 一款基于PHP+MYSQL的开源内容管理系统，模板标签简单、可扩展性强，利于二次开发，轻松建站的首选利器。
 *
 * @package     QZLOG
 * @license     http://www.qzlog.com/license
 * @link        http://www.qzlog.com
 * @mail        info@shyohan.com
 * @since       Version 3.0
 * @copyright   Copyright (c) 2015 - 2017 上海优翰信息科技有限公司. All rights reserved.
 */

defined('IN_QZLOG') OR exit('QZLOG:No direct script access allowed!');

/**
 * 菜单管理
 * @author LINK <link@shyohan.com>
 */
class MenuController extends QzController
{
    private $db;

    public function __init()
    {
        $this->db = K("Menu");
    }

    /**
     * 主页
     * @author LINK <link@shyohan.com>
     */
    public function index()
    {
        $pid = Q('get.id', 0);
        if ($pid) {
            $up_menu = $this->db->where(array('id' => $pid))->getField('title');
        } else {
            $up_menu = '';
        }
        $data = $this->db->where(array('pid' => $pid))->order("sort ASC")->all();
        $all_menu = $this->db->getMenusIdTitle();
        int_to_string($data, array('hide' => array(1 => '是', 0 => '否'), 'is_dev' => array(1 => '是', 0 => '否')));
        if ($data) {
            foreach ($data as &$key) {
                if ($key['pid']) {
                    $key['up_title'] = $all_menu[$key['pid']];
                } else {
                    $key['up_title'] = '无';
                }
            }
        }
        $bread = make_bread(array('菜单管理' => '', '菜单列表' => ''));
        $this->assign('data', $data);
        $this->assign('pid', $pid);
        $this->assign('up_menu', $up_menu);
        $this->assign('bread', $bread);
        $this->display();
    }

    /**
     * 添加
     * @author LINK <link@shyohan.com>
     */
    public function add()
    {
        if (IS_POST) {
            if ($this->db->addMenu()) {
                $this->update_menus();
                qz_ajax_return("新增成功!");
            } else {
                qz_ajax_return_error($this->db->error);
            }
        } else {
            $data = $this->db->order("sort ASC,id ASC")->all();
            $data = Data::tree($data, "title", "id", "pid");
            array_unshift($data, array('id' => 0, '_name' => '顶级菜单'));
            $this->assign('menus', $data);
            $this->display();
        }
    }

    /**
     * 编辑
     * @author LINK <link@shyohan.com>
     */
    public function edit()
    {
        if (IS_POST) {
            if ($this->db->editMenu()) {
                $this->update_menus();
                qz_ajax_return("编辑成功!");
            } else {
                qz_ajax_return_error($this->db->error);
            }
        } else {
            $id = Q('id');
            $qz = $this->db->find($id);
            $data = $this->db->order("sort ASC,id ASC")->all();
            $data = Data::tree($data, 'title', 'id', 'pid');
            array_unshift($data, array('id' => 0, '_name' => '顶级菜单'));
            $this->assign('qz', $qz);
            $this->assign('menus', $data);
            $this->display();
        }
    }

    /**
     * 删除 & 批量删除
     * @author LINK <link@shyohan.com>
     */
    public function del()
    {
        if ($this->db->delMenu()) {
            $this->update_menus();
            qz_ajax_return("删除成功!");
        } else {
            qz_ajax_return_error($this->db->error);
        }
    }

    /**
     * 菜单状态修改
     * @author LINK <link@shyohan.com>
     */
    public function changeStatus()
    {
        if ($this->db->changeStatusMenu()) {
            $this->update_menus();
            qz_ajax_return("操作成功!");
        } else {
            qz_ajax_return_error($this->db->error);
        }
    }
}
